﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ERP.Data
{
    public class Cate
    {

        public Cate()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        sERPEntities data = new sERPEntities();
        Common commonclass = new Common();

        #region Đơn vị tính

        public bool AddDVT(Dm_Donvitinh entity)
        {
            data.AddToDm_Donvitinh(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool UpdateDVT(Dm_Donvitinh entity)
        {
            //data.Dm_Donvitinh.AddObject(entity);
            return data.SaveChanges() > 0 ? true : false;
            
        }

        public bool DelDVT(Dm_Donvitinh entity)
        {
            data.DeleteObject(entity);return data.SaveChanges() > 0 ? true : false;
        }

        public DataTable loadDSDVTAll()
        {
            var result = from a in data.Dm_Donvitinh
                         select new 
                            { 
                                a.IdDonvitinh,
                                a.MaDonvitinh, 
                                a.TenDonvitinh 
                            };

            var dt = commonclass.LINQToDataTable(result);
            dt.TableName = "SalesDmDonvitinhEntity";
            return dt;
        }

        public Dm_Donvitinh loadDVTById(long iddvt)
        {
            var result = from a in data.Dm_Donvitinh
                         where a.IdDonvitinh == iddvt
                         select a;

            return result.Single();
        }

        #endregion

        #region Group Hàng hóa

        #region Nhóm hàng hóa

        public bool AddNhomhanghoa(Dm_Nhom_Hanghoa entity)
        {
            data.AddToDm_Nhom_Hanghoa(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool UpdateNhomhanghoa(Dm_Nhom_Hanghoa entity)
        {
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool DelNhomhanghoa(Dm_Nhom_Hanghoa entity)
        {
            data.DeleteObject(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public DataTable loadDSNhomhanghoaAll()
        {
            var result = from a in data.Dm_Nhom_Hanghoa
                         select new
                         {
                             a.IdNhomHanghoa,
                             a.MaNhomHanghoa,
                             a.TenNhomHanghoa
                         };

            var dt = commonclass.LINQToDataTable(result);
            dt.TableName = "Dm_Nhom_Hanghoa";
            return dt;
        }

        public Dm_Nhom_Hanghoa loadNhomhanghoaById(long idNhomhanhoa)
        {
            var result = from a in data.Dm_Nhom_Hanghoa
                         where a.IdNhomHanghoa == idNhomhanhoa
                         select a;

            if (result != null)
            {
                return result.Single();
            }
            return null;
        }

        #endregion

        #region Loại hàng hóa

        public bool AddLoaihanghoa(Dm_Loai_Hanghoa entity)
        {
            data.AddToDm_Loai_Hanghoa(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool UpdateLoaihanghoa(Dm_Loai_Hanghoa entity)
        {
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool DelLoaihanghoa(Dm_Loai_Hanghoa entity)
        {
            data.DeleteObject(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public DataTable loadDSLoaihanghoaAll()
        {
            var result = from a in data.Dm_Loai_Hanghoa
                         select new
                         {
                             a.IdLoaiHanghoa,
                             a.IdNhomHanghoa,
                             a.MaLoaiHanghoa,
                             a.TenLoaiHanghoa,
                             a.Nxt,
                             a.Hinh
                         };

            var dt = commonclass.LINQToDataTable(result);
            dt.TableName = "Dm_Loai_Hanghoa";
            return dt;
        }

        public List<Dm_Loai_Hanghoa> loadListLoaihanghoaAll()
        {
            var result = from a in data.Dm_Loai_Hanghoa
                         select a;
            return result.ToList();
        }

        public List<Dm_Loai_Hanghoa> loadListLoaihanghoabyIdNhomHH(long idNhomHanghoa)
        {
            var result = from a in data.Dm_Loai_Hanghoa
                         where a.IdNhomHanghoa == idNhomHanghoa
                         select a;
            return result.ToList();
        }

        public Dm_Loai_Hanghoa loadLoaihanghoabyIdLoaiHH(long idloaihanghoa)
        {
            var result = from a in data.Dm_Loai_Hanghoa
                         where a.IdLoaiHanghoa == idloaihanghoa
                         select a;

            if (result != null)
            {
                return result.Single();
            }
            return null;
        }
        
        #endregion

        #region hàng hóa

        public bool Addhanghoa(Dm_Hanghoa entity)
        {
            data.AddToDm_Hanghoa(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool Updatehanghoa(Dm_Hanghoa entity)
        {
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool Delhanghoa(Dm_Hanghoa entity)
        {
            data.DeleteObject(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public DataTable loadDSHanghoaAll()
        {

            var result = from a in data.Dm_Hanghoa
                         join b in data.Dm_Loai_Hanghoa on a.IdLoaiHanghoa equals b.IdLoaiHanghoa
                         select new
                         {
                             a.IdHanghoa,
                             a.MaHanghoa,
                             a.TenHanghoa,
                             a.IdLoaiHanghoa,
                             a.IdDonvitinh,
                             a.Hinh,
                             a.Thongtinthem,
                             a.Giaban,
                             a.Giamua,
                             a.SoluongTon,
                             b.IdNhomHanghoa
                         };

            var dt = commonclass.LINQToDataTable(result);
            dt.TableName = "Dm_Hanghoa";
            return dt;
        }

        public List<Dm_Hanghoa> loadListHanghoaAll()
        {

            var result = from a in data.Dm_Hanghoa
                         join b in data.Dm_Loai_Hanghoa on a.IdLoaiHanghoa equals b.IdLoaiHanghoa
                         select a;


            return result.ToList();
        }

        public List<Dm_Hanghoa> loadListHanghoaByIdLoaiHH(long idLoaihanghoa)
        {

            var result = from a in data.Dm_Hanghoa
                         where a.IdLoaiHanghoa == idLoaihanghoa
                         select a;


            return result.ToList();
        }

        public Dm_Hanghoa loadHanghoaByMahanghoa(string maHanghoa)
        {

            var result = from a in data.Dm_Hanghoa
                         where a.MaHanghoa == maHanghoa
                         select a;

            return result.ToList().Count > 0 ? result.SingleOrDefault() : new Dm_Hanghoa();
        }

        public Dm_Hanghoa loadHanghoaById(long idhanghoa)
        {

            var result = from a in data.Dm_Hanghoa
                         where a.IdHanghoa == idhanghoa
                         select a;

            return result.ToList().Count > 0 ? result.SingleOrDefault() : new Dm_Hanghoa();
        }
        
        #endregion

        #endregion

        #region Kho hàng

        public bool AddKho(Dm_Kho entity)
        {
            data.AddToDm_Kho(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool UpdateKho(Dm_Kho entity)
        {
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool DelKho(Dm_Kho entity)
        {
            data.DeleteObject(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public DataTable loadDSKhoAll()
        {

            var result = from a in data.Dm_Kho
                         select new
                         {
                             a.IdKho,
                             a.MaKho,
                             a.TenKho,
                             a.Thongtinthem
                         };

            var dt = commonclass.LINQToDataTable(result);
            dt.TableName = "Dm_Kho";
            return dt;
        }

        public Dm_Kho loadKhoById(long idKho)
        {

            var result = from a in data.Dm_Kho
                         where a.IdKho == idKho
                         select a;

            if (result != null)
            {
                return result.Single();
            }
            return null;
        }
        
        #endregion

        #region Khách hàng

        public bool AddKhachhang(Dm_Khachhang entity)
        {
            data.AddToDm_Khachhang(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool UpdateKhachhang(Dm_Khachhang entity)
        {
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool DelKhachhang(Dm_Khachhang entity)
        {
            data.DeleteObject(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public DataTable loadDSKhachhangAll()
        {

            var result = from a in data.Dm_Khachhang
                         select new
                         {
                             a.IdKhachhang,
                             a.MaKhachhang,
                             a.TenKhachhang,
                             a.Diachi,
                             a.Dienthoai,
                             a.DinhmucNo,
                             a.Masothue,
                             a.Ngaysinh,
                             a.Thongtinthem
                         };

            var dt = commonclass.LINQToDataTable(result);
            dt.TableName = "loadDSKhachhangAll";
            return dt;
        }

        public Dm_Khachhang loadKhachhangByMaKh(string maKhachhang)
        {

            var result = from a in data.Dm_Khachhang
                         where a.MaKhachhang == maKhachhang
                         select a;

            return result.ToList().Count > 0 ? result.First() : new Dm_Khachhang();
        }

        public Dm_Khachhang loadKhachhangByIdKh(long idkhachhang)
        {

            var result = from a in data.Dm_Khachhang
                         where a.IdKhachhang == idkhachhang
                         select a;

            return result.ToList().Count > 0 ? result.First() : new Dm_Khachhang();
        }

        #endregion

        #region Nhà cung cấp

        public bool AddNCC(Dm_Nhacungcap entity)
        {
            data.AddToDm_Nhacungcap(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool UpdateNCC(Dm_Nhacungcap entity)
        {
            return data.SaveChanges() > 0 ? true : false;
        }

        public bool DelNCC(Dm_Nhacungcap entity)
        {
            data.DeleteObject(entity);
            return data.SaveChanges() > 0 ? true : false;
        }

        public DataTable loadDSNCCAll()
        {

            var result = from a in data.Dm_Nhacungcap
                         select new
                         {
                             a.IdNhacungcap,
                             a.MaNhacungcap,
                             a.TenNhacungcap,
                             a.Diachi,
                             a.Dienthoai,
                             a.DinhmucNo,
                             a.Masothue,
                             a.Thongtinthem
                         };

            var dt = commonclass.LINQToDataTable(result);
            dt.TableName = "SalesDmNCCEntity";
            return dt;
        }

        public Dm_Nhacungcap loadNCCById(long idnhacungcap)
        {

            var result = from a in data.Dm_Nhacungcap
                         where a.IdNhacungcap == idnhacungcap
                         select a;

            if (result != null)
            {
                return result.Single();
            }
            return null;
        }

        #endregion


    }
}
